package com.lcz.suanfa.面试;

import org.junit.Test;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.TreeSet;

/**
 * <pre>
 * <img width="128" height="110" src="https://tse3-mm.cn.bing.net/th/id/OIP-C.en-w_oH-yn2UsSRfWnOsGAHaGY?w=198&h=180&c=7&r=0&o=5&dpr=1.25&pid=1.7" alt="马刺">
 * 作者:     <a href="https://github.com/liuchuanzheng">刘传政</a>
 * 创建时间:  17:04 2022/8/12 0012
 * QQ:      1052374416
 * 电话:     18501231486
 * 描述:
 * </pre>
 */
public class 公司一面试题 {
    @Test
    public void test() {

        System.out.println(isPrime(4));
    }

    public void aa() {
        System.out.println("aa");
    }

    /**
     * 是否是质数
     * 大于1的数,只有1和本身能整除
     *
     * @param n
     * @return
     */
    public int isPrime(long n) {
        int isPrime = 1;
        if (n == 1 || n == 2) {
            isPrime = 1;
            return isPrime;
        }
        for (int i = 3; i * i <= n; i++) {
            if (n % i == 0) {
                isPrime = i;
                return isPrime;
            }
        }
        return isPrime;
    }


    public int stockPairs(int[] stocksProfit, int target) {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> path = new ArrayList<>();

        //去重
        TreeSet<Integer> set = new TreeSet<>();
        for (int i : stocksProfit) {
            set.add(i);
        }
        int[] arrays = new int[set.size()];
        int i = 0;
        for (Integer integer : set) {
            arrays[i] = integer;
            i++;
        }
        //排序
        Arrays.sort(arrays);
        treeSearch(arrays, target, path, 0, result);
        return result.size();
    }

    /**
     * 树状结构展开每一个可能,把正确的可能保存下来
     *
     * @param arrays 源头数组
     * @param target 和
     * @param path   记录搜索过程中的路径
     * @param index  当前从源头数组的开始下标
     * @param result 当前成功的路径的保存
     */
    public void treeSearch(int[] arrays, int target, List<Integer> path, int index, List<List<Integer>> result) {
        if (target == 0) {
            result.add(new ArrayList<>(path));
        }
        //直接跳过此数
        treeSearch(arrays, target, path, index + 1, result);
        //选择当前数,继续走
        if (target - arrays[index] >= 0) {
            //路径添加一个元素
            path.add(arrays[index]);
            treeSearch(arrays, target - arrays[index], path, index, result);
            //路径删掉刚添加的元素,以便重用
            path.remove(path.size() - 1);
        }

    }

    public String[] processLogs(String[] logs, int maxSpan) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        HashMap<Integer, Long[]> map = new HashMap<>();
        //遍历,分解信息后,添加到map中
        for (int i = 0; i < logs.length; i++) {
            String log = logs[i];
            String[] split = log.split(" ");
            if (split.length != 3) {
                break;
            }
            int userId = Integer.parseInt(split[0]);
            long time = Long.parseLong(split[1]);
            String type = split[2];
            if (map.get(userId) == null) {
                map.put(userId, new Long[3]);
            }
            Long[] value = map.get(userId);
            if (type.equals("sign-in")) {
                value[0] = time;
            } else if (type.equals("sign-out")) {
                value[1] = time;
            }
            //这里没考虑多次登录的情况
            if (value[0] != null && value[1] != null) {
                long delta = value[1] - value[0];
                value[2] = delta;
            }
        }
        for (int s : map.keySet()) {
            if (map.get(s)[2] != null && map.get(s)[2] <= 20 ) {
                arrayList.add(s);
            }
        }
        //排序
        Collections.sort(arrayList);
        if (arrayList.size() > 0) {
            String[] strings = new String[arrayList.size()];
            for (int i = 0; i < arrayList.size(); i++) {
                strings[i] = String.valueOf(arrayList.get(i));
            }
            return strings;
        } else {
            return new String[0];
        }


    }
}
